<template>
<div>
    <Form ref="form">
        <Row :gutter="16" type="flex">
            <Col>
            <FormItem>
                <DatePicker transfer v-width="200" type="daterange" v-model="timer" placeholder="日期选择" @on-change="askData" />
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Input v-width="210" @on-enter="askDatas" @on-clear="askData" clearable v-model="params.keywords"
                    placeholder="请输入患者姓名/手机号/诊疗号" />
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="150" v-model="params.belong_assistant_id" placeholder="所属医助 " :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.member_medical_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="100" v-model="params.outpatient_type_id" placeholder="就诊类型" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.outpatient_type_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="100" v-model="params.effect_type_id" placeholder="效果类型" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.effect_type_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="100" v-model="params.risk_type_id" placeholder="风控类型" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.risk_type_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="110" v-model="params.clinic_id" placeholder="请选择诊室" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.clinic_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-model="params.body_parts_ids" placeholder="请选择部位" filterable multiple
                    @on-change="askData">
                    <Option v-for="item in baseInfo.body_parts_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-model="params.tags_ids" filterable multiple placeholder="请选择标签"
                    @on-change="askData">
                    <OptionGroup v-for="item in baseInfo.tags_list" :key="item.id" :label="item.name">
                        <Option v-for="items in item.children" :value="items.id" :key="items.id" :disabled="+items.status === 0">
                            {{ items.name }}</Option>
                    </OptionGroup>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="115" v-model="params.patient_diseases_id" placeholder="请选择病种" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.patient_diseases_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem>
                <Select transfer v-width="140" v-model="params.fault_diagnosis_type" placeholder="请选择断诊次数" :clearable="true"
                    @on-change="askData">
                    <Option :value="1">1次</Option>
                    <Option :value="2">2次</Option>
                    <Option :value="3">3次</Option>
                    <Option :value="4">3次以上</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
                <FormItem>
                    <Select transfer v-width="140" v-model="params.in_hospital" placeholder="请选择住院状态" :clearable="true"
                        @on-change="askData">
                        <Option v-for="item in baseInfo.hospital" :key="item.value" :value="item.value" >{{item.label}}</Option>
                    </Select>
                </FormItem>
            </Col>
        </Row>
    </Form>
    <tableList
        :edit_type="2"
        :params="params"
        :loading="loading"
        :data="data"
        :total="total"
        @askData="askData"
        @sortchange="sortchange" 
    />
</div>
</template>
<script>
    import { PatientList } from '@/api/patient'
    import tableList from './table-list';
    export default {
        components: { tableList },
        props: ['typeList', 'labelList'],
        data() {
            return {
                timer: [],
                params:  {
                    start_time: '',
                    end_time: '',
                    keywords: '',
                    belong_assistant_id: '',
                    outpatient_type_id: '',
                    effect_type_id: '',
                    risk_type_id: '',
                    clinic_assess_id: '',
                    body_parts_ids: [],
                    clinic_id: '',
                    tags_ids: [],
                    sort_last_revisiting_type: 0,
                    sort_next_revisiting_type: 0,
                    sort_last_callback_type: 0,
                    sort_next_callback_type: 0,
                    sort_revisiting_type: 0,
                    sort_callback_type: 0,
                    sort_missed_type: 0,
                    sort_patient_card_id: 0,
                    page: 1,
                    pageSize: 10,
                    patient_diseases_id: '',
                    fault_diagnosis_type: '',
                    in_hospital:''
                },
                loading: false,
                baseInfo: {
                    body_parts_list: [],
                    tags_list: [],
                    member_list: [],
                    effect_type_list: [],
                    risk_type_list: [],
                    clinic_assess_list: [],
                    outpatient_type_list: [],
                    patient_diseases_list: [],
                    member_medical_list: [],
                    hospital:[]
                },
                grid: {
                    xl: 4,
                    lg: 8,
                    md: 12,
                    sm: 24,
                    xs: 24
                },
                data: [],
                total: 0,
            }
        },
        mounted() {
            let jumpPage = window.localStorage.getItem('jumpDetail')
            if (jumpPage) {
                let parsePage = JSON.parse(jumpPage)
                this.params.belong_assistant_id = parsePage.belong.toString()
            }
            this.initial()
        },
        methods: {
            initial() {
                this.askData()
            },
            askData() {
                this.loading = true
                let params = JSON.parse(JSON.stringify(this.params))
                params.end_time = this.timer[1]?this.$Date(this.timer[1]).format('YYYY-MM-DD'):'',
                params.start_time = this.timer[0]?this.$Date(this.timer[0]).format('YYYY-MM-DD'):'',
                params.body_parts_ids = this.params.body_parts_ids.join(','),
                params.tags_ids = this.params.tags_ids.join(','),
                PatientList(params).then(res => {
                    this.total = res.total
                    this.data = res.list
                    this.$emit('alltotal', {num: 1, total: res.total})
                    for (let i = 0; i < this.data.length; i++) {
                        this.$set(this.data[i], 'show', false)
                    }
                    if (this.baseInfo.body_parts_list.length === 0 &&
                        this.baseInfo.clinic_assess_list.length === 0 &&
                        this.baseInfo.effect_type_list.length === 0 &&
                        this.baseInfo.member_list.length === 0 &&
                        this.baseInfo.outpatient_type_list.length === 0 &&
                        this.baseInfo.risk_type_list.length === 0 &&
                        this.baseInfo.tags_list.length === 0) {
                        this.baseInfo = res.condition
                    }
                }).catch(() => {
                    this.loading = false
                }).finally(() => {
                    this.loading = false
                })
            },
            askDatas() {
                this.timer = []
                this.params.start_time = ''
                this.params.end_time = ''
                this.params.belong_assistant_id = ''
                this.params.outpatient_type_id = ''
                this.params.effect_type_id = ''
                this.params.risk_type_id = ''
                this.params.clinic_assess_id = ''
                this.params.body_parts_ids = []
                this.params.clinic_id = ''
                this.params.tags_ids = []
                this.params.sort_last_revisiting_type = 0
                this.params.sort_next_revisiting_type = 0
                this.params.sort_last_callback_type = 0
                this.params.sort_next_callback_type = 0
                this.params.sort_revisiting_type = 0
                this.params.sort_callback_type = 0
                this.params.sort_missed_type = 0
                this.params.page = 1
                this.params.pageSize = 10
                this.params.patient_diseases_id = ''
                this.params.fault_diagnosis_type = ''
                this.params.in_hospital=''
                this.askData()
            },
            sortchange(e) {
                this.params.sort_last_revisiting_type = 0
                this.params.sort_next_revisiting_type = 0
                this.params.sort_last_callback_type = 0
                this.params.sort_next_callback_type = 0
                this.params.sort_revisiting_type = 0
                this.params.sort_callback_type = 0
                this.params.sort_missed_type = 0
                this.params.sort_patient_card_id = 0
                switch (e.key) {
                    case 'patient_card_id':
                        this.params.sort_patient_card_id = e.sort
                        break;
                    case 'last_revisiting_time_str':
                        this.params.sort_last_revisiting_type = e.sort
                        break;
                    case 'next_revisiting_time_str':
                        this.params.sort_next_revisiting_type = e.sort
                        break;
                    case 'last_callback_time_str':
                        this.params.sort_last_callback_type = e.sort
                        break;
                    case 'next_callback_time_str':
                        this.params.sort_next_callback_type = e.sort
                        break;
                    case 'revisiting_count':
                        this.params.sort_revisiting_type = e.sort
                        break;
                    case 'missed_days':
                        this.params.sort_missed_type = e.sort
                        break;
                    case 'callback_count':
                        this.params.sort_callback_type = e.sort
                        break;
                }
                this.askData()
            }
        },
    }
</script>
